/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



public class Gun extends Thread{
    protected int damage;
    protected final int MAX_BULLETS;
    protected int bullets;
    protected int clips;
    protected int reloadtime;
    protected String name;
    private boolean reloading;
    private Thread reloadthread;
    
    Runnable runner = new Runnable(){
        public void run() {
            reloading = true;
            try {
               sleep(1000);
            }catch (InterruptedException e){}
            if(clips < MAX_BULLETS){
                bullets = clips;
                clips = 0;
            }
            else{
                clips -= 20 - bullets;
                bullets = MAX_BULLETS;
            }
            reloading = false;
            Main.app.gui.nifty.getCurrentScreen().findElementByName("status").hide();
            reloadthread.stop();
        }
    };
    public Gun(int gundamage, int max, int time, String gunname){
        damage = gundamage;
        MAX_BULLETS = max;
        bullets = MAX_BULLETS;
        clips = MAX_BULLETS * 5;
        reloadtime = time;
        name = gunname;
        reloading = false;
    }
    public void reset(){
        bullets = MAX_BULLETS;
        clips = MAX_BULLETS * 5;
    }
    public String getGunName(){return name;}
    public int getBullets(){return bullets;}
    public int getClips(){return clips;}
    public void reload(){
        if(!reloading){
            if(bullets < MAX_BULLETS && clips > 0){
                Main.app.gui.nifty.getCurrentScreen().findElementByName("status").show();
                reloadthread = new Thread(runner);
                reloadthread.start();
            }
        }
    }
    
    public void shoot(){
        if(!reloading){
            if(bullets <= 0) reload(); 
            else {
                Main.app.audio_gun.playInstance();
                bullets--;
            }
        }
    }

    public boolean getReloading() {return reloading;}

    public void addAmmo(int i) {clips += i;}

    public int getDamage() {return damage;}
    
}
